﻿using Atlanchim.Crm.Mui.Helpers;
using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Atlanchim.Crm.Mui.Pages.Alertes
{
    public class PageViewAlerteModel : AbstractPageViewModel
    {
        /// <summary>
        /// Contact de référence
        /// </summary>
        public Alerte Alerte { get; set; }

        /// <summary>
        /// Url d'accès à la page d'édition
        /// </summary>
        public override string EditUrl { get; set; }


        /// <summary>
        /// FullName du Contact
        /// </summary>
        public string ContactFullName
        {
            get
            {
                if (this.Alerte.Contact == null)
                {
                    return "Indéfini";
                }
                else
                {
                    return string.Format("{0} {1}", this.Alerte.Contact.Nom, this.Alerte.Contact.Prenom);
                }
            }
        }

        /// <summary>
        /// Lien vers la page contact
        public string ContactViewUrl
        {
            get
            {
                return string.Format("/Pages/Contacts/PageViewContact.xaml#IdContact={0}", this.Alerte.Contact.Id);
            }
        }

        /// <summary>
        /// Chargement
        /// </summary>
        public override void Load()
        {
            // Lecture du fragment
            int idAlerte = 0;
            int.TryParse(this.DicFragments["IdAlerte"], out idAlerte);

            // Chargement
            using (CrmContext context = new CrmContext())
            {
                // Alerte
                this.Alerte = context
                                .Alertes
                                .Include("Contact")
                                .Where(alerte => alerte.Id == idAlerte)
                                .FirstOrDefault();
                this.EditUrl = string.Format("/Pages/Alertes/PageEditAlerte.xaml#IdAlerte={0}", idAlerte);
            }

            // Notification
            OnPropertyChanged("Alerte");
        }

        /// <summary>
        /// Suppression
        /// </summary>
        protected override void OnDeleteCurrentEntity()
        {
            CrmCrudHelper.DeleteEntityNewContext(this.Alerte);
        }
    }
}
